<template>
  <div :class="['login-wrapper', ['', 'login-form-right', 'login-form-left'][direction]]" >
    <div class="login-title">博学乐育智慧校园安全管理平台</div>
    <el-form
      ref="form"
      size="large"
      :model="form"
      :rules="rules"
      class="login-form"
      @keyup.enter.native="doSubmit">
      <div class="login-form-title"> 
        <div class="t0">欢迎登录</div>
        <div class="t1">智慧启迪心灵 科学创造未来！</div>
      </div>
      <el-form-item prop="username">
        <el-input
          clearable
          v-model="form.username"
          prefix-icon="el-icon-user"
          :placeholder="$t('login.username')"/>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          show-password
          v-model="form.password"
          prefix-icon="el-icon-lock"
          :placeholder="$t('login.password')"/>
      </el-form-item>
      <el-form-item prop="captcha">
        <div class="login-input-group">
          <el-input
            clearable
            v-model="form.captcha"
            prefix-icon="el-icon-_vercode"
            :placeholder="$t('login.captcha')"/>
          <img
            alt=""
            v-if="captcha"
            :src="captcha"
            @click="changeCode"
            class="login-captcha"/>
        </div>
      </el-form-item>
      <div class="el-form-item">
        <el-checkbox v-model="form.remember">{{ $t('login.remember') }}</el-checkbox>
      </div>
      <div class="el-form-item">
        <el-button
          size="large"
          type="primary"
          class="login-btn"
          :loading="loading"
          @click="doSubmit">
          {{ loading ? $t('login.loading') : '立即登录' }}
        </el-button>
      </div>
    </el-form>
    <div class="login-copyright"><a href="https://beian.miit.gov.cn" style="color: #eee" target="_blank">粤ICP备2024272167号 版本v1.0.0</a></div>
  </div>
</template>

<script>
import setting from '@/config/setting';
import {removeAllPageTab} from "@/utils/page-tab-util";

export default {
  name: 'Login',
  data() {
    return {
      // 登录框方向, 0居中, 1居右, 2居左
      direction: 0,
      // 加载状态
      loading: false,
      // 表单数据
      form: {
        username: '',
        password: '',
        captcha: '',
        key: '',
        remember: true
      },
      // 验证码base64数据
      captcha: '',
    };
  },
  computed: {
    // 表单验证规则
    rules() {
      return {
        username: [
          {required: true, message: this.$t('login.username'), type: 'string', trigger: 'blur'}
        ],
        password: [
          {required: true, message: this.$t('login.password'), type: 'string', trigger: 'blur'}
        ],
        captcha: [
          {required: true, message: this.$t('login.captcha'), type: 'string', trigger: 'blur'}
        ]
      };
    }
  },
  mounted() {
    if (setting.takeToken()) {
      this.goHome();
    } else {
      this.changeCode();
    }
  },
  methods: {
    /* 提交 */
    doSubmit() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;
        this.$http.post('/login/login', this.form).then((res) => {
          this.loading = false;
          if (res.data.code === 0) {
            this.$message.success('登录成功');
            this.$store.dispatch('user/setToken', {
              token: 'Bearer ' + res.data.data.access_token,
              remember: this.form.remember
            }).then(() => {
this.tabRemoveAll();
              this.goHome();
            });
          } else {
            this.$message.error(res.data.msg);
            // 重新刷新验证码
            this.changeCode()
          }
        });
      });
    },
	tabRemoveAll(active) {
		removeAllPageTab();
		if (active !== '/') {
		this.$router.push('/');
		}
	},
    /* 跳转到首页 */
    goHome() {
      const query = this.$route.query;
      const path = query && query.from ? query.from : '/';
      this.$router.push(path).catch(() => {
      });
    },
    /* 更换图形验证码 */
    changeCode() {
      // 这里演示的验证码是后端返回base64格式的形式
      this.$http.get('/login/captcha').then(res => {
        if (res.data.code === 0) {
          this.captcha = res.data.data.captcha;
          this.form.key = res.data.data.key;
          this.$refs.form.clearValidate();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch((e) => {
        this.$message.error(e.message);
      });
    }
  }
}
</script>

<style scoped>
/* 背景 */
.login-wrapper {
  padding: 42px 58px;
  position: relative;
  box-sizing: border-box;
  background-image: url("https://xckj-auto-file-res.oss-cn-shenzhen.aliyuncs.com/uploads/shoubiao/WechatIMG95.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  min-height: 100vh;
}
.login-wrapper .login-title{
  font-size: 48px;
  color: #FFFFFF;
  line-height: 67px;
}
.login-wrapper:before {
  content: "";
  /* background-color: rgba(0, 0, 0, .2); */
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

/* 卡片 */
.login-form {
float: right;
  width: 380px;
  max-width: 100%;
  padding: 25px 30px;
  position: relative;
  box-sizing: border-box;
  z-index: 2;
  background: rgba(255,255,255,0.3);
  box-shadow: inset 0px 1px 30px 0px #FFFFFF;
  border-radius: 23px;
}
.login-form-title .t0{
  font-weight: 500;
  font-size: 24px;
  color: #FFFFFF;
  line-height: 62px;
  text-align: left;
  position: relative;
  
}
.login-form-title .t0::after{
    content:"";
    position: absolute;
    background: #FFFFFF;
    border-radius: 3px;
    opacity: 0.35;
    bottom:5px;
    left:0;
    width: 80px;
    height: 6px;
  }
.login-form-title .t1{
  font-size: 20px;
  color: #FFFFFF;
  line-height: 62px;
  text-align: left;
}
.login-form-right .login-form {
  margin: 0 15% 0 auto;
}

.login-form-left .login-form {
  margin: 0 auto 0 15%;
}

.login-form h4 {
  text-align: center;
  margin: 0 0 25px 0;
}

.login-form > .el-form-item {
  margin-bottom: 25px;
}

/* 验证码 */
.login-input-group {
  display: flex;
  align-items: center;
}

.login-input-group ::v-deep .el-input {
  flex: 1;
}

.login-captcha {
  height: 38px;
  width: 102px;
  margin-left: 10px;
  border-radius: 4px;
  border: 1px solid #DCDFE6;
  text-align: center;
  cursor: pointer;
}

.login-captcha:hover {
  opacity: .75;
}

.login-btn {
  display: block;
  width: 100%;
}

/* 第三方登录图标 */
.login-oauth-icon {
  color: #FFF;
  padding: 5px;
  margin: 0 10px;
  font-size: 18px;
  border-radius: 50%;
  cursor: pointer;
}

/* 底部版权 */
.login-copyright {
  color: #eee;
  padding-top: 20px;
  text-align: center;
  position: relative;
  z-index: 1;
}

/* 响应式 */
@media screen and (min-height: 550px) {
  .login-form {
    position: absolute;
    top: 50%;
    /* left: 50%; */
    right:0!important;
    transform: translateX(-50%);
    margin-top: -220px;
  }

  .login-form-right .login-form,
  .login-form-left .login-form {
    left: auto;
    right: 15%;
    transform: translateX(0);
    margin: -220px auto auto auto;
  }

  .login-form-left .login-form {
    right: auto;
    left: 15%;
  }

  .login-copyright {
    position: absolute;
    bottom: 20px;
    right: 0;
    left: 0;
  }
}

@media screen and (max-width: 768px) {
  .login-form-right .login-form,
  .login-form-left .login-form {
    left: 50%;
    right: auto;
    transform: translateX(-50%);
    margin-right: auto;
  }
}
</style>
